// index.ts
import { WXAPI } from '../../app'
const drawQrcode = require('../../utils/weapp.qrcode.min.js')
// 获取应用实例
// const app = getApp<IAppOption>()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseGetUserProfile: false,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName'), // 如需尝试获取用户信息可改为false
    shifouplay: false,
    music: {},
    currentTab: 0
  },
  // 事件处理函数
  bindViewTap() {
    wx.switchTab({
      url: '../logs/logs',
    })
  },

  //点击切换
  clickTab: function (e: { target: { dataset: { current: number; name: string }; }; }) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      this.getList(e.target.dataset.name)
      that.setData({
        currentTab: e.target.dataset.current
      })
      return true;
    }
  },

  onLoad() {
    // @ts-ignore
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }

    drawQrcode({
      width: 200,
      height: 200,
      canvasId: 'myQrcode',
      text: 'https://u.wechat.com/MFKeJidHqxxi6FKrO7wWnfs'
    })

    WXAPI.banners().then((res: { code: number; data: any }) => {
      if (res.code == 0) {
        this.setData({
          banners: res.data
        })
        console.log(res.data)
      }
    })

    this.getList("热歌榜")

  },
  getUserProfile() {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e: any) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },

  getList: function (name: string) {
    // 利用小程序内置的发送请求的方法
    wx.request({
      url: `https://api.uomg.com/api/rand.music?sort=${name}&format=json`, //接口地址
      success: (res) => {
        // console.log(res)
        let resData = res.data as any
        if (res.errMsg = "request:ok") {
          // console.log(resData.data)
          this.setData({
            music: resData.data
          });
        }
      } // 请求成功的回调函数
    })
  },

  playMusic(_ev: any) {
    var shifouplay = this.data.shifouplay;
    var music = this.data.music as any;
    console.log(music)
    var bgm = wx.getBackgroundAudioManager()
    console.log(bgm)
    if (shifouplay) {
      bgm.pause();
      // wx.pauseBackgroundAudio();
      this.setData({
        shifouplay: false
      })
    } else {
      bgm.title = music.name || '告白';
      bgm.epname = music.name || '告白';
      bgm.singer = music.artistsname || '沈以诚';
      bgm.coverImgUrl = music.picurl || 'http://p3.music.126.net/4xF8d6QNMy-TKYMy2AUwCQ==/109951163803974764.jpg';
      // 设置了 src 之后会自动播放
      bgm.src = music.url || 'http://music.163.com/song/media/outer/url?id=1336856449';
      bgm.play();
      // wx.playBackgroundAudio({
      //   dataUrl: 'http://music.163.com/song/media/outer/url?id=1336856449',
      //   title: '告白',
      //   coverImgUrl: 'http://p3.music.126.net/4xF8d6QNMy-TKYMy2AUwCQ==/109951163803974764.jpg'
      // })
      this.setData({
        shifouplay: true
      })
    }
  }

})
